﻿using System;
using System.Linq;
using System.Windows.Forms;
using CoreMobile.bce;
using Opticon.Bcrctl;
using VABMobile.oo;

namespace VABMobile.ui
{
    public partial class RegViajeForm : Form, IBCENotifiable
    {
        private Turno turno=null;
        private BCEReader reader;


        public RegViajeForm(Persona aux)
        {
            InitializeComponent();

            //initData(aux);

            //loadInForm();
            turno=new Turno("<nil>",null,null,null,aux,null);
            this.auxiliar.Text = aux.ppRut();
            reader = BCEReader.getReaderWith(this);
        }

        private void loadInForm()
        {
            //this.turnoId.Items.Add(turno.Id);
            //this.turnoId.SelectedIndex = 0;
            this.busPatente.Text = turno.Bus.Patente;
            this.rutChofer1.Text = turno.Chofer1.ppRut();
            this.rutChofer2.Text = turno.Chofer1.ppRut();
            this.auxiliar.Text = turno.Auxiliar.ppRut();
            this.origen.Text = turno.Itinerario.Origen;
            this.destino.Text = turno.Itinerario.Destino;
        }

        private void initData(Persona aux)
        {
            throw new NotSupportedException("Recepcion de comandos no soportado");
            /*var protocol = new VABProtocol();
            Client net = Client.getDefaultClient();
            net.connect();

            ArrayList objects=protocol.commandReceive(
                net.sendAndReceive(
                    protocol.commandSend(new LoginCommand(),aux)));

            turno = (Turno) objects[0];
            turno.Auxiliar = aux;
            net.disconnect();*/
        }

        private void viajarMenu_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(this.turnoId.Text) ||
                string.IsNullOrEmpty(this.busPatente.Text) ||
                string.IsNullOrEmpty(this.rutChofer1.Text))
            {
                MessageBox.Show("Debe ingresar servicio, bus y por lo menos un chofer!");
                return;
            }

            /*try
            {
                turno.Id = Int32.Parse(this.turnoId.Text);
            }
            catch (Exception)
            {

                MessageBox.Show("Turno debe ser un numero");
                return;
            }*/
            turno.Id = this.turnoId.Text;
            
            turno.Bus=new Bus(this.busPatente.Text);
            (new PasajerosForm(turno)).Show();
            this.Hide();
            reader.BarcodeDecoder.DataHandler -= this.receiverDataFromReader;
        }

        private void salirMenu_Click(object sender, EventArgs e)
        {
            MessageBox.Show("No Disponible, debe reg. viaje 1ero");
        }

        private void service_Click(object sender, EventArgs e)
        {
            var cfg = new ServiceNameForm();

            if (DialogResult.OK == cfg.ShowDialog())
            {
                this.turnoId.Text = cfg.ServiceName;
                this.turno.Itinerario = Itinerario.itinerarios()
                    .FirstOrDefault(itinerario => 
                        cfg.SelectedServiceName.Substring(0,3) == itinerario.Codigo);
                this.turno.Itinerario.Sentido = cfg.SelectedServiceName[3];
                this.origen.Text = turno.Itinerario.Origen;
                this.destino.Text = turno.Itinerario.Destino;
            }
        }

        public void receiverDataFromReader(object sender, BarcodeDecoder.ScanEventArgs e)
        {
            string data = reader.preProcess(e);

            //MessageBox.Show(data);

            if (string.IsNullOrEmpty(data)) return;

            if (e.CodeType != BarcodeDecoder.CodeIdTypes.PDF417)
            {
                //MessageBox.Show("Necesita leer un CI del Reg. Civil");
                return;
            }

            if(! this.rutChofer1.Focused &&! this.rutChofer2.Focused )
            {
                MessageBox.Show("Necesita selecionar campo chofer 1 o 2");
                return;
            }
            string rut = BCEReader.decodePDF417RegCivil(data)[0];
            if(this.rutChofer1.Focused)
            {
                turno.Chofer1= new Persona(rut);
                this.rutChofer1.Text = turno.Chofer1.ppRut();
                return;
            }
            
                turno.Chofer2= new Persona(rut);
                this.rutChofer2.Text = turno.Chofer2.ppRut();
        }
    }
}